const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({

  data: {
    userInfo:'',
    newmobile:'',
    code2:'',
    code:'',
    mobile:'',
    timer: '', //定时器名字
    countDownNum: '60', //倒计时初始值
    codeDis: false,
    telcode: '发送验证码',
    timer2: '', //定时器名字
    countDownNum2: '60', //倒计时初始值
    codeDis2: false,
    telcode2: '发送验证码',
    uuid:'',
    uuid2:'',
  },
  onLoad: function (options) {
    this.info()
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          userInfo:res.data.baseData,
          mobile:res.data.baseData.mobile
        })
      }
    })
  },
  countDown: function() {
    let that = this;
    let countDownNum = that.data.countDownNum;
    that.setData({
      timer: setInterval(function() {
        countDownNum--;
        that.setData({
          countDownNum: countDownNum,
          codeDis: true
        })
        if (countDownNum <= 0) {
          that.setData({
            codeDis: false,
            countDownNum: 60
          })
          clearInterval(that.data.timer);
        }
      }, 1000)
    })
  },
  countDown2: function() {
    let that = this;
    let countDownNum2 = that.data.countDownNum2;
    that.setData({
      timer2: setInterval(function() {
        countDownNum2--;
        that.setData({
          countDownNum2: countDownNum2,
          codeDis2: true
        })
        if (countDownNum2 <= 0) {
          that.setData({
            codeDis2: false,
            countDownNum2: 60
          })
          clearInterval(that.data.timer2);
        }
      }, 1000)
    })
  },
  //发送短信验证码
  sendCode: function() {
    if (this.data.mobile == '') {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!(/^1[123456789]\d{9}$/.test(this.data.mobile))) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    // 获取数据
    Controller.sendChangeCode({
      mobile: this.data.mobile
    }).then(res => {
      this.setData({
        uuid:res.data.baseData.uuid
      })
      wx.showToast({
        title: '发送成功！',
        icon: 'success',
        duration: 1500
      })
      this.countDown();
    });
  },
  //发送短信验证码
  sendCode2: function() {
    // if(!this.data.code){
    //   wx.showToast({
    //     title: '请获取当前账号的验证码',
    //     icon: 'none',
    //     duration: 1500
    //   })
    // }
    if (this.data.newmobile == '') {
      wx.showToast({
        title: '新的手机号码不能为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!(/^1[123456789]\d{9}$/.test(this.data.newmobile))) {
      wx.showToast({
        title: '新的手机号码错误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    // 获取数据
    Controller.sendChangeCode({
      mobile: this.data.newmobile
    }).then(res => {
      this.setData({
        uuid2:res.data.baseData.uuid
      })
      wx.showToast({
        title: '发送成功！',
        icon: 'success',
        duration: 1500
      })
      this.countDown2();
    });
  },
  changeinput(e){
    let data = {};
    data[e.currentTarget.dataset.name]=e.detail.value
    this.setData(data)
  },
  bindMobile(){
    Controller.bindMobile({
      mobile: this.data.newmobile,
      code:this.data.code2,
      uuid:this.data.uuid2 || 12333
    }).then(res => {
      wx.showToast({
        title: '设置成功！',
        icon: 'success',
        duration: 1500
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      },1500)
    });
  },
  onShareAppMessage: function () {

  }
})